package com.itechhero.gencode.service.impl;

import com.itechhero.common.utils.poi.ExcelUtil;
import com.itechhero.gencode.domain.GenTable;
import com.itechhero.gencode.domain.GenTableColumn;
import com.itechhero.gencode.mapper.GenTableColumnMapper;
import com.itechhero.gencode.mapper.GenTableMapper;
import com.itechhero.gencode.service.DBExcelService;
import com.itechhero.gencode.util.poi.DBExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @description:
 * @author: godkey
 * @Date: 2020/12/7 10:12
 */
@Service
public class DBExcelServiceImpl implements DBExcelService {

    @Autowired
    private GenTableMapper genTableMapper;
    @Autowired
    private GenTableColumnMapper genTableColumnMapper;

    /**
     * 将数据库表信息导出Excel
     */
    public void exportDBExcel(){
        // 从数据库中查询所有表名
        ArrayList<GenTable> genTables = genTableMapper.selectDbTableAllList();
//        String[] tableNames = new String[genTables.size()];
//        List<String> collect = genTables.stream().map(GenTable::getTableName).collect(Collectors.toList());
//        collect.toArray(tableNames);

        genTables.forEach(genTable -> {
            // 根据表名查询列信息
            List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(genTable.getTableName());
            genTable.setColumns(genTableColumns);
        });

        // 根据查询的数据库字段信息生成Excel表格
        ExcelUtil excelUtil = new DBExcelUtil(genTables);
        excelUtil.importTemplateExcel("支撑平台表格");
    }

}
